package com.hrt.manage.role.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Role;
import com.hrt.bean.UserRoles;
import com.hrt.manage.role.dao.RoleDao;

@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao {

	@Override
	public Long totalCount() {
		// TODO Auto-generated method stub
		String hql = "select count(c) from Role c where c.isdelete=0";
		List result = this.getHibernateTemplate().find(hql);
		if (result != null && result.size() > 0) {
			return new Long(result.get(0).toString());
		} else {
			return 0l;
		}

	}

	@Override
	public Long totalCount(Map<String, Object> condition) {
		// TODO Auto-generated method stub
		String hql = "select count(c) from Role c where c.isdelete=0";
		if (condition != null && condition.size() > 0) {

			for (Iterator<Entry<String, Object>> itr = condition.entrySet()
					.iterator(); itr.hasNext();) {
				Entry entry = itr.next();
				String fieldName = entry.getKey().toString();
				String value = entry.getValue().toString();
				hql = hql + " and c." + fieldName + value + " ";
			}

		}
		System.out.println("hql---------------------->" + hql);
		List result = getHibernateTemplate().find(hql);
		if (result != null && result.size() > 0) {
			return new Long(result.get(0).toString());
		} else {
			return 0l;
		}
	}

	/**
	 * 
	 */
	@Override
	public Role getRoleByid(Long roleId) {

		final String hql = "select c from Role c where c.id=" + roleId
				+ " and c.isdelete=0";
		return getHibernateTemplate().execute(new HibernateCallback<Role>() {

			@Override
			public Role doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery(hql);

				return (Role) q.uniqueResult();
			}

		});

	}

	/**
	 * 
	 */
	@Override
	public List<Role> queryByPage(int pageIndex, int pageSize) {

		List<Role> list = new ArrayList<Role>();

		String hql = "select c from Role c where c.isdelete=0";

		list = this.queryByPage(hql, pageIndex, pageSize);

		return list;

	}

	@Override
	public String queryRoleByPinYin(String pinyin) {
		// TODO Auto-generated method stub
		final String hql = "select c.rCode from Role c where c.isdelete=0 and c.rCode='"
				+ pinyin + "'";
		return this.getHibernateTemplate().execute(
				new HibernateCallback<String>() {

					@Override
					public String doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query = session.createQuery(hql);
						Object result = query.uniqueResult();
						if (result != null) {
							return result.toString();
						} else {
							return null;
						}
					}

				});
	}

	@Override
	public List<Role> listRole() {
		List<Role> list = new ArrayList<Role>();

		String hql = "select c from Role c where c.isdelete=0";
		list = this.getHibernateTemplate().find(hql);
		return list;
	}

	@Override
	public List<Role> queryRoleWithOutUsers(Long userId) {
		List<Role> list = new ArrayList<Role>();
		String hql = "SELECT r from Role r  WHERE r.id NOT IN(SELECT ur.roleId FROM UserRoles ur WHERE ur.userId="+userId+" AND ur.isdelete=0)";
		hql = hql + " AND r.isdelete=0";

		list = this.getHibernateTemplate().find(hql);
		
		return list;
	}

	@Override
	public List<Role> getRoleByid(Long[] ids) {
		List<Role> list = new ArrayList<Role>();
		
		String hql = "SELECT r from Role r  WHERE r.id  IN(";
		for(int i=0;i<ids.length;i++){
			if(i<ids.length-1){
			hql = hql +ids[i]+",";
			}
			else{
				hql = hql +ids[i]+")";
			}
		}
		hql = hql + " AND r.isdelete=0";

		list = this.getHibernateTemplate().find(hql);
		return list;
	}

	// @Override
	// public List<Role> list() {
	// // TODO Auto-generated method stub
	// return null;
	// }
}
